package sctu.edu.java.year2016.team07.project.admin;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

/**
 * Created by Huang Suixin on 2016/6/30.
 */
public class AdminFrame extends JFrame{
    public AdminFrame() throws HeadlessException {
        init();

    }
    private void init (){
        //设置窗口背景
        try {
            ImageIcon background = new ImageIcon(ImageIO.read((new File("src\\main\\java\\sctu\\edu\\java\\year2016\\team07\\project\\image\\push_background.jpg"))));
            JLabel label = new JLabel(background);
            label.setBounds(0, 0, background.getIconWidth(), background.getIconHeight());
            JPanel imagePanel = (JPanel) this.getContentPane();     //将内容窗格式转化成JPanel，否则不能使用方法setOpaque()来使内容窗格透明
            imagePanel.setOpaque(false);         //使用方法setOpaque()来是内容窗格透明
            imagePanel.setLayout(new FlowLayout());
            getLayeredPane().setLayout(null);

            getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));

        } catch (IOException e) {
            e.printStackTrace();
        }

        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        setBounds(150, 150, 800, 600);
        setTitle("用户反馈信息");
        setResizable(false);        //设置窗口不可改变
        setAlwaysOnTop(true);

    }

    //装入整个面板的每一个小面板，每个面板都有一条显示反馈的label ，和查看这条信息的button
    protected class Pan extends JPanel{
        public Pan(String recordfilename) {
            JLabel jl = new JLabel(recordfilename);
            jl.setFont(new Font("dialog", 1, 20));
            jl.setForeground(Color.ORANGE);     //设置字体颜色
            JButton button = new JButton("查看");
            button.setFont(new Font("dialog", 1, 20));
            button.setForeground(Color.ORANGE);
            button.setContentAreaFilled(false);
            button.setBorderPainted(false);
            add(jl);
            add(button);
            setOpaque(false);


            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Dialog dialog = new Dialog(recordfilename);
                    dialog.setLocation(getWidth(),getHeight());
                    dialog.setVisible(true);
                }
            });

        }
    }

    //查看反馈问题的弹窗
    protected class Dialog extends JDialog{
        public Dialog(String recordFile) {
            Server server = new Server();
            Container con = getContentPane();
            JPanel pan = new JPanel();      //整个大的容器
            pan.setOpaque(false);
            pan.setLayout(new GridLayout(2,0));

            //文本容器
            JPanel textPan =new JPanel();
            textPan.setOpaque(false);
            //按钮容器
            JPanel buttonPan = new JPanel();
            buttonPan.setOpaque(false);

            //初始化
            setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            setTitle(recordFile);
            setSize(600, 500);
            setAlwaysOnTop(true);

            //文本区域
            JTextArea area = new JTextArea(10,25);
            area.setFont(new Font("dialog", 1, 20));
            area.setOpaque(false);
            area.setEditable(false);      //设置文本框不可编辑
            area.setText(server.getContent(recordFile));
            JScrollPane sp = new JScrollPane(area);
            sp.setOpaque(false);
            sp.getViewport().setOpaque(false);

            //解决按钮，并添加事件
            JButton solve = new JButton("已解决");
            solve.setFont(new Font("dialog", 1, 20));       //美化按钮
            solve.setContentAreaFilled(false);
            solve.setBorderPainted(false);
            solve.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    server.deleteFile(recordFile);
                    dispose();
                }
            });

            //关闭按钮，并添加事件
            JButton close = new JButton("知道了");
            close.setFont(new Font("dialog", 1, 20));
            close.setContentAreaFilled(false);
            close.setBorderPainted(false);
            close.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    dispose();
                }
            });

            textPan.add(sp);
            buttonPan.add(solve);
            buttonPan.add(close);
            pan.add(textPan);
            pan.add(buttonPan);
            con.add(pan);
            //设置背景
            try {
                ImageIcon background = new ImageIcon(ImageIO.read((new File("src\\main\\java\\sctu\\edu\\java\\year2016\\team07\\project\\image\\land_background.jpg"))));
                JLabel label = new JLabel(background);
                label.setBounds(0, 0, background.getIconWidth(), background.getIconHeight());
                JPanel imagePanel = (JPanel) this.getContentPane();     //将内容窗格式转化成JPanel，否则不能使用方法setOpaque()来使内容窗格透明
                imagePanel.setOpaque(false);         //使用方法setOpaque()来是内容窗格透明
                imagePanel.setLayout(new FlowLayout());
                getLayeredPane().setLayout(null);

                getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
